package com.chookss.home.gallery;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.entity.GallerySearchEntity;
import com.chookss.home.gallery.adapter.GallerySearchAddressAdapter;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.RecycleViewDivider;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

public class GallerySearchAddressListActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.rvView)
    RecyclerView recyclerView;
    @BindView(R.id.srlView)
    SmartRefreshLayout smrRf;
    @BindView(R.id.ivNone)
    ImageView ivNone;
    @BindView(R.id.tvNone)
    TextView tvNone;
    @BindView(R.id.tvNone2)
    TextView tvNone2;
    @BindView(R.id.llNone)
    LinearLayout llNone;
    @BindView(R.id.llBg)
    LinearLayout llBg;
    private GallerySearchAddressAdapter adapter;
    private int currentPage = 1;
    private boolean isEnd = false;//是否到底
    private int searchType = 1;//  搜索类型 1-日期 2-标签 3 地点 默认值为1
    private List<GallerySearchEntity.CityListBean.ListBean> list = new ArrayList<>();
    //筛选标签
    private String searchKeyWord, title;
    private String startTime = "", endTime = "", provides = "", orgCodes="",catalogCodes = "", tagCodes = "", citys = "", provinces = "", fileTypes = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_gallery_lists);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        title = getIntent().getStringExtra("title");
        searchKeyWord = getIntent().getStringExtra("searchKeyWord");
        startTime = getIntent().getStringExtra("startTime");
        endTime = getIntent().getStringExtra("endTime");
        provides = getIntent().getStringExtra("provides");
        orgCodes = getIntent().getStringExtra("orgCodes");
        catalogCodes = getIntent().getStringExtra("catalogCodes");
        tagCodes = getIntent().getStringExtra("tagCodes");
        citys = getIntent().getStringExtra("citys");
        provinces = getIntent().getStringExtra("provinces");
        fileTypes = getIntent().getStringExtra("fileTypes");
        searchType = getIntent().getIntExtra("searchType", 1);
        if (Utils.isNull(title)) {
            commonTitleTxt.setText("地点");
        } else {
            commonTitleTxt.setText(title);
        }
        ivNone.setBackgroundResource(R.drawable.icon_default_no_common);
        tvNone.setText("暂无数据");
        recyclerView.setPadding(Utils.dip2px(this, 12), Utils.dip2px(this, 12), 0, 0);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager
                .VERTICAL, false));
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, Utils.dip2px(this, 12), getResources().getColor(R.color.white)));
        adapter = new GallerySearchAddressAdapter(R.layout.item_gallery_search_result_tag, this, list);
        recyclerView.setAdapter(adapter);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        smrRf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isEnd) {//has next page
                    MyToast.show("没有更多数据了");
                    loadEnd();
                } else {
                    currentPage++;
                    getData();
                }
            }
        });
        smrRf.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                currentPage = 1;
                isEnd = false;
                getData();
            }
        });
        smrRf.autoRefresh();
    }

    private void getData() {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("searchType", searchType + "");
        if (!Utils.isNull(searchKeyWord)) {
            allmap.put("searchKeyWord", searchKeyWord);
        }
        if (!Utils.isNull(startTime)) {
            allmap.put("startTime", startTime);
        }
        if (!Utils.isNull(endTime)) {
            allmap.put("endTime", endTime);
        }
        if (!Utils.isNull(provides)) {
            allmap.put("provideEmployeeCodes", provides);
        }
        if (!Utils.isNull(orgCodes)) {
            allmap.put("orgCodes", orgCodes);
        }
        if (!Utils.isNull(catalogCodes)) {
            allmap.put("catalogCodes", catalogCodes);
        }
        if (!Utils.isNull(tagCodes)) {
            allmap.put("tagCodes", tagCodes);
        }
        if (!Utils.isNull(provinces)) {
            allmap.put("imageUploadLocationProvinces", provinces);
        }
        if (!Utils.isNull(citys)) {
            allmap.put("imageUploadLocationCitys", citys);
        }
        if (!Utils.isNull(fileTypes)) {
            allmap.put("fileTypes", fileTypes);
        }
        allmap.put("pageNum", currentPage + "");
        allmap.put("pageSize", "20");
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<GallerySearchEntity>>() {
        }.getType(), null, Urls.selectAppImageSearchList, SignUtils2.makeSignStr(allmap), false, new ObserverCallback<GallerySearchEntity>() {
            @Override
            public void onSuccess(GallerySearchEntity data) {
                if (currentPage == 1) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                }
                if (smrRf == null || llNone == null) {
                    return;
                }
                adapter.setSearchData(searchKeyWord, title, startTime, endTime, provides,orgCodes, catalogCodes, tagCodes, provinces, citys, fileTypes);
                int oldIndex = list.size();
                if (data.getCityList() != null) {
                    list.addAll(data.getCityList().getList());
                    if (currentPage == 1) {
                        adapter.notifyDataSetChanged();
                    } else {
                        adapter.notifyItemRangeChanged(oldIndex, data.getCityList().getList().size());
                    }
                    smrRf.setVisibility(View.VISIBLE);
                    llNone.setVisibility(View.GONE);
                } else {
                    isEnd = true;
                    if (list.size() == 0) {
                        smrRf.setVisibility(View.GONE);
                        llNone.setVisibility(View.VISIBLE);
                    }

                }
                loadEnd();
            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }

    private void loadEnd() {
        if (null != smrRf && smrRf.getState() == RefreshState.Refreshing)
            smrRf.finishRefresh(true);
        if (null != smrRf && smrRf.getState() == RefreshState.Loading)
            smrRf.finishLoadMore(true);
    }

}
